package org.example.struct.array;

import java.util.*;

/**
 * @author liyishan
 * @date 2024/8/13 10:17
 * @apiNote
 */

public class Test00819MostCommonWord {
    public static void main(String[] args) {
        String paragraph = "Bob";
        String[] banned ={};
        String res  = mostCommonWord(paragraph, banned);
        System.out.println(res);
    }

    public static String mostCommonWord(String paragraph, String[] banned) {
        Set<String> bannedSet = new HashSet<>(Arrays.asList(banned));
        Map<String,Integer> map = new HashMap<>();
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i <= paragraph.length(); i++){
            if(i< paragraph.length() && Character.isLetter(paragraph.charAt(i))){
                sb.append(Character.toLowerCase(paragraph.charAt(i)));
            }else if (sb.length() > 0) {
                String word = sb.toString();
                if(!bannedSet.contains(word)){
                    map.put(word, map.getOrDefault(word, 0) + 1);
                }
                sb.setLength(0);
            }
        }
        String result = "";
        int maxCount = 0;
        for(Map.Entry<String, Integer> entry : map.entrySet()){
            String word = entry.getKey();
            if(entry.getValue() > maxCount){
                maxCount = entry.getValue();
                result = word;
            }
        }
        return result;
    }
}
